<?php
// 测试网站

class CeshiAction extends CommonAction
{
    /**
    +----------------------------------------------------------
    * 默认操作
    +----------------------------------------------------------
    */
    public function index()
    {
        echo '这是一个测试';
        //$this->moniter();
        $this->detection();
       // print_r($this->netcs());
        


        /*$t = 3;
       for ($i=0;$i < $t;$i++)  
 	   {
 	    $ceshi_ad = M("runtime");
 	    $dat=date("Y-m-d H:i:s");
 	    print_r($dat);
 	    $ceshi_ad->execute("update baomu_runtime set runtime='$dat' where time_id=1"); 	    
 	    $ceshi = M("site");
        $where['active'] = 1;
        $b = $ceshi->where($where)->select();
        //echo count($b).'<br>';
        $t = count($b);
        $a = $ceshi->select($i);        
       // print_r($b);
        echo '<pre>';
        print_r($a);
        echo '<pre>';
        $url = 'http://'.$a[0][domain];//网址
        $s_date = date("Y-m-d H:i:s");//时间
        $this->http_p($url);//开启测试
        $b_date = date("Y-m-d H:i:s");//时间
        $sb=strtotime($b_date)-strtotime($s_date);
        echo '<h2>'.$sb.'</h2>';
        $this->display(THINK_PATH.'/Tpl/Autoindex/hello.html');
        //sleep(50);
 	   }*/

    }
    /**
    +----------------------------------------------------------
    * 网站链接测试
    +----------------------------------------------------------
    */
    public function http_p($url,$method,$postDate)
    {
    	
        $method ='GET';
        $postDate = '';
        import("ORG.Net.WebRequest");//导入测试类
        $ht_tp = new WebRequest($url,$method,$postDate);

        if (!$c=$ht_tp->getResponse())
        {
        	return 'not open';
        }
        else {
        //临时输出
        echo '<h2>'.'测试wewweI'.$i.'</h2>';
        echo '<pre>';
        print_r($c);
        echo '<pre>';
        
        echo '<h1>'.$c->status.'</h1>';//状态，200ok；
//        $Model = M('record');
//        $data['status_code']=$c->status;
//        $data['userid']='';
//        $data['siteid']='';
//        $data['type']='';
//        $data['size']='';
//        $data['taketime']='';
//        $data['addtime']='';
//        $Model->data($data)->add();        
        echo '<br>';
        echo $c->headers[domain];//二级数组示例      
        return $c->status;
        }
    }
    
    /**
    +----------------------------------------------------------
    * 循环
    +----------------------------------------------------------
    */
    
    public function moniter()
    {
    	ignore_user_abort(); // run script in background

        set_time_limit(0); // run script forever

        $interval=100; // do every 15 minutes…循环间隔变量
        
        do{
          // * 调用检测函数-----------------*/
       
          $this->detection();
       //----------------------------------    
           sleep($interval); // wait 15 minutes循环间隔（睡眠时间）

          }while(true);
    
      }
      
      //检测
      public function detection()
      {
          //$t = 3;
           $ceshi = M("site");
           $where['active'] = 1;
           $b = $ceshi->where($where)->select();
           
        //echo count($b).'<br>';
           $t = count($b)+1;  
      	
           for ($i=0;$i <= $t;$i++)  
 	      {
 	       $ceshi_ad = M("runtime");
 	       $dat=date("Y-m-d H:i:s");
 	       print_r($dat);
 	       $ceshi_ad->execute("update baomu_runtime set runtime='$dat' where time_id=1");  	      
           echo '<h2>'.'这是检测网站总数'.$t.'</h2>';
           //$a = $ceshi->select($i); 
           echo '<pre>';       
        print_r($b);
        echo '<pre>';
        //print_r($a);
        echo '<pre>';
        
           $url = 'http://'.$b[$i][domain];//网址
           $s_date = date("Y-m-d H:i:s");//时间
           $status=$this->http_p($url);//开启测试
          $b_date = date("Y-m-d H:i:s");//时间
          $sb=strtotime($b_date)-strtotime($s_date);//------
          
          echo '<h2>'.'标识I'.$i.'</h2>';
          echo '<h2>'.'时间'.$sb.'</h2>';
          echo '<h2>'.'状态'.$status.'</h2>';
          echo '<h2>'.'这是什么？'.$b[$i][domain].'</h2>';
          echo '<h2>'.'网站ID'.$b[$i][siteid].'</h2>';
          
          $ip=$this->g_ip($b[$i][domain]);
          echo '<h2>'.'检测IP'.$ip.'</h2>';
          
          $Model = M('record');
          $data['status_code']=$status;
          $data['userid']='';
          $data['siteid']=$b[$i][siteid];
          $data['type']='';
          $data['size']='';
          $data['taketime']=$sb;
          $data['addtime']=strtotime(date("Y-m-d H:i:s"));
          $data['site_ip']=$ip;
          $Model->data($data)->add(); 
          
          $site= M('site');
          $site->create();
          $ssdate['status_code']=$status;
          $wwe['siteid']=$b[$i][siteid];
          $site->data($ssdate)->where($wwe)->save();
          
          $this->display(THINK_PATH.'/Tpl/Autoindex/hello.html');
        //sleep(10);
 	      }
      	
      }
        
    	public function netcs()
    	{
    		 import("ORG.Net.Http");
    		 $chttp = new Http();
    		 $ip=$chttp->get_header_info();
    		 return $ip;    		
    	}
    	//获取IP
    	public function g_ip($add)
    	{
    	  //$add ='www.baidu.com';
          if($a = gethostbyname($add))
          {
            return $a;
          }
          else 
          {
          	return 'no find';
          }        
    	}


  

}
?>

